﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace igflow
{
    public class Scene
    {
        public List<GameObject.igGameObject> gameObjects;
        public Scene()
        {
            gameObjects = new List<igflow.GameObject.igGameObject>();
        }

        public void Update()
        {
            {
                CameraManager.CurrentCamera.SetOffset(new Vector3(Input.RightStick(),0));
                if (Input.Left())
                {
                    GameObject.igGameObject o = FindObject("Selection");
                    if (o != null)
                    {
                        //o.Position += new Vector3(-3.0f, 0, 0);
                        CameraManager.PreviousMount();
                    }
                }
                if (Input.Right())
                {
                    GameObject.igGameObject o = FindObject("Selection");
                    if (o != null)
                    {
                        //o.Position += new Vector3(3.0f, 0, 0);
                        CameraManager.NextMount();
                    }
                }

                foreach (GameObject.igGameObject gameObject in gameObjects)
                {
                    gameObject.SendMessage("Update");

                    gameObject.SendMessage("LateUpdate");


                }

                // non alphaed
                foreach (GameObject.igGameObject gameObject in gameObjects)
                {
                    if (gameObject.name == "Ground")
                    {
                        int a = 0;
                    }
                    gameObject.SendMessage("Draw");
                }

                // alphaed
                foreach (GameObject.igGameObject gameObject in gameObjects)
                {
                    //gameObject.SendMessage("Draw");
                }
            }
        }

        private GameObject.igGameObject FindObject(string p)
        {
            foreach (GameObject.igGameObject obj in gameObjects)
            {
                if (obj.name == p)
                {
                    return obj;
                }
            }
            return null;
        }

        internal void AddGameObject(igflow.GameObject.igGameObject gameObject)
        {
            gameObjects.Add(gameObject);
        }

        internal void Init()
        {
            /*
TestGUIObject o = new TestGUIObject();
o.Position += new Vector3(0 , 0, 0);
AddGameObject(o);
*/

            //GameObject.igGameObject b = new igflow.GameObject.igGameObject("Selection");

            // Set up Renderer and mesh
            /*
            Components.igMeshRenderer renderer = new igflow.Components.igMeshRenderer();
            renderer.meshes.Add(new RectMesh(2.0f, 2.0f, 0.1f, "pink"));
            b.AddComponent(renderer);

            // Set the position
            b.Position = new Vector3(0.0f, 0, 0.5f);

            // Add the gameobject
            AddGameObject(b);

            /**/
            /*
            testObject c = new testObject("pink");
            c.Position = new Vector3(-3.0f, 0, 0);
            AddGameObject(c);*/

            // Ground
            GameObject.igGameObject Ground = new igflow.GameObject.igGameObject("Ground");
            igflow.Components.igMeshRenderer q = new igflow.Components.igMeshRenderer();
            Ground.AddComponent(q);
            Ground.Position += new Vector3(0, -1.0f, 0);
            q.meshes.Add(new QuadMesh(30.0f, 30.0f));
            Ground.Rotation = Quaternion.CreateFromYawPitchRoll(0, MathHelper.ToRadians(-90.0f), MathHelper.ToRadians(90.0f));
            AddGameObject(Ground);

            igNode node = new igNode(new Vector3(-2.0f, 0, 0.05f), "NODEA");
            node.Init();
            node.AddLink(new igLink(new Vector3(0.0f, 0, 0.05f), "NODEB"));
            /// FONT OBJECT
            /// 
            //TextObject t = new TextObject(new Vector3(-2.0f, 0, 0.05f), "EXIT", new Vector2(0.15f, 0.15f));
            //AddGameObject(t);

            TextObject y = new TextObject(new Vector3(0.0f, 0, 0.05f), "EDIT", new Vector2(0.15f, 0.15f));
            //AddGameObject(y);

            TextObject u = new TextObject(new Vector3(2.0f, 0, 0.05f), "CONTESTS", new Vector2(0.15f, 0.15f));
            //AddGameObject(u);

            TextObject i = new TextObject(new Vector3(4.0f, 0, 0.05f), "ANIMAL", new Vector2(0.15f, 0.15f));
            //AddGameObject(i);
        }
    }
}
